window.onload = function() {
	var ChannelId=getItems("Channel") != null ? getItems("Channel") : "mychannel";
	var FabricFlag=getItems("networkName");
	var Token=getItems("token");

	if(getUrlParam('StartIndex') == null && getUrlParam('BlockHash') == null){
		if(loginToken){
			//查询块高度
			var queryChannelInfoUrl = '/api/block/public/queryChannelInfo';
			var queryChannelInfoData = {
				"RequestType": "QueryChannelInfoReq",
				"ChannelId":ChannelId,
				"FabricFlag":FabricFlag
			};
			$.ajax({
				type: "POST",
				contentType: "application/json",
				url: queryChannelInfoUrl,
				data: JSON.stringify(queryChannelInfoData),
				dataType: "json",
				headers:{
					Authorization:Token,
				},
				success: function(data) {
					if(data.code==200){
						var oData = JSON.parse(data.data);
						var total= oData.ChannelInfo.Heigth-1 == -1?'0':oData.ChannelInfo.Heigth-1;
						var oStr='<div class="positionLeft fl">共'+total+'条</div>';
						$('.m-style').before(oStr);
						$('.m-style').pagination({
							pageCount:Math.ceil(oData.ChannelInfo.Heigth/5),
							totalData: total,
							mode: 'fixed',
							prevContent: '上一页',
							nextContent: '下一页',
							jump: true,
							isHide:false,
							callback: function (api) {   
									var NowIndex = api.getCurrent();
									if(loginToken){
										var uri="/api/block/public/queryBlock";
									}else{
										var uri="/api/block/cache/queryBlock";
									}
									$(".cover").show();
									pageData(((NowIndex-1) * 5) + 1,uri);
							}
						});
						if(oData.ChannelInfo.Heigth == 0){
							$(".m-style").hide();
						}
						// 初始化数据
						requestPost()
					}else{
						showHtml();
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					showHtml();
					$(".cover").hide();
					console.log("调取sns接口出错");
					console.log(XMLHttpRequest.status);
					console.log(XMLHttpRequest.readyState);
					console.log(textStatus);
				}
			});
		}else{
			//查询块高度
			var queryChannelInfoUrl = '/api/block/cache/queryChannelInfo';
			var queryChannelInfoData = {
				"RequestType": "QueryChannelInfoReq",
				"ChannelId":ChannelId,
				"FabricFlag":FabricFlag
			};
			$.ajax({
				type: "POST",
				contentType: "application/json",
				url: queryChannelInfoUrl,
				data: JSON.stringify(queryChannelInfoData),
				dataType: "json",
				headers:{
					Authorization:Token,
				},
				success: function(data) {
					if(data.code==200){
						var oData = JSON.parse(data.data);
						var total= oData.ChannelInfo.Heigth-1 == -1?'0':oData.ChannelInfo.Heigth-1;
						var oStr='<div class="positionLeft fl">共'+total+'条</div>';
						$('.m-style').before(oStr);
						$('.m-style').pagination({
							pageCount:Math.ceil(oData.ChannelInfo.Heigth/5),
							totalData: total,
							mode: 'fixed',
							prevContent: '上一页',
							nextContent: '下一页',
							jump: true,
							isHide:false,
							callback: function (api) {   
								// console.log(api.getCurrent())
									var NowIndex = api.getCurrent();
									if(loginToken){
										var uri="/api/block/public/queryBlock";
									}else{
										var uri="/api/block/cache/queryBlock";
									}
									$(".cover").show();
									pageData(((NowIndex-1) * 5) + 1,uri);
							}
						});
						if(oData.ChannelInfo.Heigth == 0){
							$(".m-style").hide();
						}
					}else{
						showHtml();
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					showHtml();
					$(".cover").hide();
					console.log("调取sns接口出错");
					console.log(XMLHttpRequest.status);
					console.log(XMLHttpRequest.readyState);
					console.log(textStatus);
				}
			});
		}

	}
	function nodeStr(oData){
		var str='';
		for(var i=0;i<oData.BlockInfoList.length;i++){
			str+='<div class="blockListCt clear"><div class="col-xs-2"><span>'+oData.BlockInfoList[i].BlockNum+'</span></div><div class="col-xs-8 text-center"><a>'+oData.BlockInfoList[i].CurrentBlockHash+'</a></div><div class="col-xs-2 text-right"><em>'+oData.BlockInfoList[i].TransactionList.length+'</em></div></div>';
		}
		$(".blockList").append(str);
	}

	function requestPost() {
		var data = {
			"RequestType": "QueryBlockByNumReq",
			"ChannelId": ChannelId,
			"FabricFlag":FabricFlag,
			"QueryBlockInfo":{
				"QueryType": 1,
				"StartIndex": 1,
				"Count":5
			}
		};
		$(".cover").show();
		$.ajax({
			type: "POST",   
			contentType: "application/json",
			url: '/api/block/public/queryBlock',
			data: JSON.stringify(data),
			dataType: "json",
			headers:{
				Authorization:Token,
			},
			success: function(data) {
				$(".cover").hide();
				if(data.code==200){
					if(data.status==40101){
						showHtml("登录已失效，请重新登录！");
					}else{
						var oData=JSON.parse(data.data);
						nodeStr(oData)
					}		
				}else{
					showHtml("暂无数据")
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				showHtml();
				$(".cover").hide();
				console.log("调取sns接口出错");
				console.log(XMLHttpRequest.status);
				console.log(XMLHttpRequest.readyState);
				console.log(textStatus);
			}
		});
	}

	function pageData(StartIndex,oUrl) {
		var queryBlockoData = {
			"RequestType": "QueryBlockByNumReq",
			"ChannelId":ChannelId,
			"FabricFlag":FabricFlag,
			"QueryBlockInfo": {
				"QueryType": 1,
				"StartIndex": StartIndex,
				"Count": 5
			}
		};
		
		$.ajax({     
			type: "POST",
			contentType: "application/json",
			url: oUrl,
			data: JSON.stringify(queryBlockoData),
			dataType: "json",
			headers:{
				Authorization:Token,
			},
			success: function(data) { 
				$(".cover").hide();
				if(data.code==200){
					if(data.data){
						var oData = JSON.parse(data.data);
						// console.log(oData)
						var str = '';
						for(var i = 0; i < oData.BlockInfoList.length; i++) {
							str += '<div class="blockListCt clear"><div class="col-xs-2"><span>' + oData.BlockInfoList[i].BlockNum + '</span></div><div class="col-xs-8 text-center"><a>' + oData.BlockInfoList[i].CurrentBlockHash + '</a></div><div class="col-xs-2 text-right"><em>' + oData.BlockInfoList[i].TransactionList.length + '</em></div></div>';
						}
						$(".blockList").html(str);
					}else{
						alert("暂无数据");
						window.location.reload();
						// console.log(112222222)
					}
					
				}else{
					alert("暂无数据!");
				}
				
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				showHtml()
				$(".cover").hide();
				console.log("调取sns接口出错");
				console.log(XMLHttpRequest.status);
				console.log(XMLHttpRequest.readyState);
				console.log(textStatus);
			}
		});
	}

	function showHtml(){
		$(".blockBox").html('<div class="NoData">暂无数据</div>');
		$(".NoData").show();
	}
	
}